package com.nt.train.vo;

import java.io.Serializable;

/**
 * 封装通用返回对象
 * @author hlf
 * @version 1.0
 * @date 2022/8/4 14:07
 */
public class ResultVo <T> implements Serializable {

    private static final long serialVersionUID = 6273326371984994386L;

    private Integer code;

    private String msg;

    private T data;

    private ResultVo() {
        this.code = 200;
        this.msg = "OK";
    }

    private ResultVo(T data) {
        this.code = 200;
        this.msg = "OK";
        this.setData(data);
    }

    private ResultVo(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ResultVo(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultVo<T> setError(Integer code, String msg) {
        this.setCode(code);
        this.setMsg(msg);
        return this;
    }

    public boolean isSuccess() {
        return this.getCode().equals(200);
    }

    public static ResultVo ok() {
        return new ResultVo();
    }

    public static <T> ResultVo ok(T data) {
        return new ResultVo(data);
    }

    public static <T> ResultVo ok(Integer code, String msg) {
        return new ResultVo(code, msg);
    }

    public static <T> ResultVo ok(Integer code, String msg, T data) {
        return new ResultVo(code, msg, data);
    }

    public static <T> ResultVo error() {
        return new ResultVo(500, "failed");
    }

    public static <T> ResultVo error(String msg) {
        return new ResultVo(500, msg);
    }

    public static <T> ResultVo error(Integer code, String msg) {
        return new ResultVo(code, msg);
    }

    public static <T> ResultVo error(Integer code, String msg, T data) {
        return new ResultVo(code, msg, data);
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
